package com.hhit.zhgd.service.impl

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.hhit.zhgd.component.AlertFileProperties
import com.hhit.zhgd.controller.AdminController
import com.hhit.zhgd.dao.ZhgWarnListMapper
import com.hhit.zhgd.dao.ZhgdAlertMapper
import com.hhit.zhgd.domain.dto.FrontAnalyzeReqDTO
import com.hhit.zhgd.domain.entity.ZhgdAlert
import com.hhit.zhgd.domain.entity.ZhgdAlertType
import com.hhit.zhgd.domain.entity.ZhgdWarnList
import com.hhit.zhgd.domain.response.BaseResp
import com.hhit.zhgd.service.ZhgdAlertTypeService
import com.hhit.zhgd.service.ZhgdAnalyzeService
import com.hhit.zhgd.utils.UtilImage
import jakarta.annotation.Resource
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service("zhgdAnalyzeService")
class ZhgdAnalyzeServiceImpl : ZhgdAnalyzeService {

    private val log= LoggerFactory.getLogger(AdminController::class.java)

    @Resource
    private var alertFileProperties: AlertFileProperties? = null

    @Autowired
    private var zhgdAlertMapper: ZhgdAlertMapper? = null

    @Autowired
    private var zhgdAlertTypeService: ZhgdAlertTypeService? = null

    @Autowired
    private var zhgdWarnListMapper: ZhgWarnListMapper? = null

    override fun frontAnalyze(frontAnalyzeReqDTO: FrontAnalyzeReqDTO): BaseResp<Any?> {

        log.info(
            "前端分析 frontAnalyze 报警数据保存并发送 deviceId:{},告警类型 WarnType:{},身份证:{},姓名:{},告警时间:{}",
            frontAnalyzeReqDTO.deviceId,
            frontAnalyzeReqDTO.warnType,
            frontAnalyzeReqDTO.faceUserid,
            frontAnalyzeReqDTO.name,
            frontAnalyzeReqDTO.capturePhoto
        )
        try {
            val imageId =
                java.lang.String.format("%s_%s.jpg", frontAnalyzeReqDTO.deviceId, System.currentTimeMillis())
            UtilImage.Base64ToImage(frontAnalyzeReqDTO.capturePhoto, alertFileProperties?.filePath, imageId)
            val warnTypeArr: List<String> = frontAnalyzeReqDTO?.warnType?.split(",")?: listOf()
            // 存数据库
            val zhgdAlert = ZhgdAlert(
                deviceId = frontAnalyzeReqDTO.deviceId,
                status = "1",
                alarmTime = frontAnalyzeReqDTO.captureTime,
                devCode = frontAnalyzeReqDTO.deviceId,
                taggingImage = "/images/$imageId",
                warnImage = "/images/$imageId",
                type = 1
            )

            val addAlertRes = zhgdAlertMapper?.insert(zhgdAlert)
            if ((addAlertRes?:0) > 0) {
                for (warnTypeStr in warnTypeArr) {
                    val queryWrapper = QueryWrapper<ZhgdAlertType>()
                    queryWrapper.eq("type",warnTypeStr)
                    val zhgdAlertType: ZhgdAlertType? = zhgdAlertTypeService?.getOne(queryWrapper)
                    if (null != zhgdAlertType) {
                        val zhgdWarnList = ZhgdWarnList(alertId = zhgdAlert.id, descc = zhgdAlertType?.typeDesc, warnGrade = "1")
                        zhgdWarnListMapper?.insert(zhgdWarnList)
                    }
                }
            }
        } catch (e: Exception) {
            log.error("前端分析 frontAnalyze 报警数据保存异常!", e)
            return BaseResp.fail("接收分析异常:" + e.message)
        }
        return BaseResp.success()
    }
}